import { createReducer, createAsyncAction } from 'redux-action-tools';
import axios from 'axios'
const instance = axios.create({
  baseURL: 'http://localhost:5000/',
  timeout: 1000,
  headers: {
    'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU3ZmNhODc3NTIxNDA1MjM5NDJjNmU0NSIsImlhdCI6MTQ4MTg1NDE4Mn0.oP_2Vd2C22aEN6Xp0qI5Hs92HXsNasPCuKPlr4ZAKhQ',
     "accept": "application/json"
  }
});
// ------------------------------------
// 
// ------------------------------------


const GETALLTERMS = 'GETALLTERMS',
  GETONETERMBYID = 'GETONETERMBYID',
  ADDONETERM='ADDONETERM';


export const getAllTerms = createAsyncAction(GETALLTERMS, (id, dispacth, getstate) => {
  return instance.get('/terms')

})
const GETALLTERMS_SUCCESS = (state, action) => {
  console.log("请求成功了",action);

  // const answerslist = [{
  //   key: 1,
  //   name: '红军长治80周年纪念答题',
  //   time: 32,
  //   address: 'New York No. 1 Lake Park',
  // }]
  return Object.assign({}, state, state.answerslist, { termslist: action.payload.data.terms })
}


const getError = (state, action) => {
  console.log("请求失败了");
  return state
}




export const getOneTermByID = createAsyncAction(GETONETERMBYID, (id, dispacth, getstate) => {
  console.log(id);
  return instance.get({})

})
const GETONETERMBYID_SUCCESS = (state, action) => {
  console.log("请求成功了");

  const answerData = [{
    key: 1,
    question_name: '红军长征开始的时间',
    description: 'sad'
  }];
  return Object.assign({}, state, state.answerData, { answerData: answerData })
}


export const addOneTerm = createAsyncAction(ADDONETERM, (id, dispacth, getstate) => {
  console.log(id);
  return fetch('http://localhost:3000/answers.json')

})
const ADDONETERM_SUCCESS = (state, action) => {
  console.log("请求成功了");

  const answerData = {
    key: 2,
    question_name: '红军长征途径的村庄',
    description: 'sad'
  };
  return Object.assign({}, state, { answerData:[...state.answerData,  answerData],questionsTableLoading:false })
}

const SIGNINWILDDOG='SIGNINWILDDOG';

export const signInWilddog=createAsyncAction(SIGNINWILDDOG,()=>{
  return 
})


const answerListReducer = createReducer()
  .when(GETALLTERMS,)
  .done(GETALLTERMS_SUCCESS)
  .failed(getError)
  .build({ termslist: [], termData: [],termsTableLoading:false,questionsTableLoading:false });

export default answerListReducer

